@echo off
chcp 65001 >nul
setlocal enabledelayedexpansion

REM Flutter版本化构建脚本 (Windows版本)
REM 用法: build_with_version.bat [平台] [构建类型]
REM 平台: android, windows, web
REM 构建类型: debug, profile, release (默认: release)

REM 获取参数
set "PLATFORM=%~1"
set "BUILD_TYPE=%~2"
if "%PLATFORM%"=="" set "PLATFORM=android"
if "%BUILD_TYPE%"=="" set "BUILD_TYPE=release"

REM 获取当前日期和时间
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YYYY=%dt:~0,4%"
set "MM=%dt:~4,2%"
set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%"
set "MI=%dt:~10,2%"
set "DATE=%YYYY%%MM%%DD%"
set "TIME=%HH%%MI%"
set "DATETIME=%DATE%_%TIME%"

REM 获取真实的应用显示名称 (从AndroidManifest.xml)
for /f "tokens=*" %%a in ('findstr "android:label=" android\app\src\main\AndroidManifest.xml') do (
    set "line=%%a"
    for /f "tokens=2 delims=^"^" %%b in ("!line!") do set "APP_NAME=%%b"
)
for /f "tokens=2" %%a in ('findstr "^version:" pubspec.yaml') do (
    set "VERSION_FULL=%%a"
    for /f "tokens=1 delims=+" %%b in ("%%a") do set "VERSION=%%b"
    for /f "tokens=2 delims=+" %%b in ("%%a") do set "BUILD_NUMBER=%%b"
)

echo.
echo 📱 Flutter版本化构建工具
echo 应用名称: %APP_NAME%
echo 版本号: %VERSION%
echo 构建号: %BUILD_NUMBER%
echo 平台: %PLATFORM%
echo 构建类型: %BUILD_TYPE%
echo 日期时间: %DATETIME%
echo.

REM 创建构建输出目录
set "BUILD_DIR=builds"
if not exist "%BUILD_DIR%" mkdir "%BUILD_DIR%"

REM Android构建
if "%PLATFORM%"=="android" (
    echo 🤖 开始构建Android APK...
    
    if "%BUILD_TYPE%"=="debug" (
        flutter build apk --debug
        set "SOURCE_FILE=build\app\outputs\flutter-apk\app-debug.apk"
    ) else if "%BUILD_TYPE%"=="profile" (
        flutter build apk --profile
        set "SOURCE_FILE=build\app\outputs\flutter-apk\app-profile.apk"
    ) else (
        flutter build apk --release
        set "SOURCE_FILE=build\app\outputs\flutter-apk\app-release.apk"
    )
    
    set "NEW_NAME=%APP_NAME%_v%VERSION%+%BUILD_NUMBER%_%BUILD_TYPE%_%DATETIME%.apk"
    set "TARGET_FILE=%BUILD_DIR%\!NEW_NAME!"
    
    if exist "!SOURCE_FILE!" (
        copy "!SOURCE_FILE!" "!TARGET_FILE!" >nul
        echo ✅ Android APK构建完成: !TARGET_FILE!
    ) else (
        echo ❌ 构建失败: 找不到APK文件
        exit /b 1
    )
)

REM Windows构建
if "%PLATFORM%"=="windows" (
    echo 🪟 开始构建Windows应用...
    
    if "%BUILD_TYPE%"=="debug" (
        flutter build windows --debug
    ) else if "%BUILD_TYPE%"=="profile" (
        flutter build windows --profile
    ) else (
        flutter build windows --release
    )
    
    set "NEW_NAME=%APP_NAME%_v%VERSION%+%BUILD_NUMBER%_windows_%BUILD_TYPE%_%DATETIME%.zip"
    set "TARGET_FILE=%BUILD_DIR%\!NEW_NAME!"
    
    if exist "build\windows\runner\Release" (
        cd build\windows\runner\Release
        powershell -command "Compress-Archive -Path '.\*' -DestinationPath '..\..\..\..\!TARGET_FILE!' -Force"
        cd ..\..\..\..
        echo ✅ Windows应用构建完成: !TARGET_FILE!
    ) else (
        echo ❌ 构建失败: 找不到Windows构建文件
        exit /b 1
    )
)

REM Web构建
if "%PLATFORM%"=="web" (
    echo 🌐 开始构建Web应用...
    
    if "%BUILD_TYPE%"=="debug" (
        flutter build web --debug
    ) else if "%BUILD_TYPE%"=="profile" (
        flutter build web --profile
    ) else (
        flutter build web --release
    )
    
    set "NEW_NAME=%APP_NAME%_v%VERSION%+%BUILD_NUMBER%_web_%BUILD_TYPE%_%DATETIME%.zip"
    set "TARGET_FILE=%BUILD_DIR%\!NEW_NAME!"
    
    if exist "build\web" (
        cd build\web
        powershell -command "Compress-Archive -Path '.\*' -DestinationPath '..\..\!TARGET_FILE!' -Force"
        cd ..\..
        echo ✅ Web应用构建完成: !TARGET_FILE!
    ) else (
        echo ❌ 构建失败: 找不到Web构建文件
        exit /b 1
    )
)

echo.
echo 🎉 构建完成！
echo 输出目录: %BUILD_DIR%
echo 生成的文件命名格式: {应用名}_v{版本号}+{构建号}_{平台}_{构建类型}_{日期时间}.{扩展名}
echo.
pause